package com.mixiaoxiao.weather.api.interceptor;

import com.blankj.utilcode.util.ResourceUtils;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class LocalApiInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        HttpUrl url = request.url();
        String urlString = url.toString();
        String fileName = "CN101010100";
        if(urlString.length() > 9) {
            String code = urlString.substring(urlString.length() - 9);
            fileName = "CN" + code;
        }
        String json = ResourceUtils.readAssets2String(fileName);
        ResponseBody body = ResponseBody.create(MediaType.parse("application/x-www-form-urlencoded"), json);
        return new Response.Builder()
                .request(request)
                .body(body)
                .protocol(Protocol.HTTP_1_1)
                .code(200)
                .message("请求成功！")
                .build();
    }
}
